class ProjectplaceRequest(object):
    method = None
    
    def __init__(self, api_path, request_handler, **kwargs):
        self.api_path = api_path
        self.request_handler = request_handler
        self.execute_request()
        
        
    def get_extra_parameters(self):
        parameter_dict = {}
        
        for param in self.request_handler.request.arguments():
            if param != 'api_path':
                parameter_dict[str(param)] = str(self.request_handler.request.get(param))
            
        return parameter_dict
    
    def execute_request(self):
        from oauth.client import OAuthClient
        
        client = OAuthClient('projectplace', self.request_handler)
        
        extra_parameters = self.get_extra_parameters()
        
        if self.method == 'GET':
            json = client.get(self.api_path, self.method, (200,), **extra_parameters)
        elif self.method in ['POST','PUT']:
            json = client.post(self.api_path, self.method, (200,), **extra_parameters)
            
        self.request_handler.response.headers["Content-Type"] = "application/json"
        
        self.request_handler.response.out.write(json)
        
class ProjectplaceGETRequest(ProjectplaceRequest):
    
    def __init__(self, api_path, request_handler, **kwargs):
        self.method = "GET"
        super(ProjectplaceGETRequest, self).__init__(api_path, request_handler, **kwargs)
        
    
class ProjectplacePOSTRequest(ProjectplaceRequest):
    
    def __init__(self, api_path, request_handler, **kwargs):
        self.method = "POST"
        super(ProjectplacePOSTRequest, self).__init__(api_path, request_handler, **kwargs)
        
class ProjectplacePUTRequest(ProjectplaceRequest):
    
    def __init__(self, api_path, request_handler, **kwargs):
        self.method = "PUT"
        super(ProjectplacePUTRequest, self).__init__(api_path, request_handler, **kwargs)        
        
        
class ProjectplaceDownloadRequest(ProjectplaceRequest):
    
    def __init__(self, api_path, version, request_handler, **kwargs):
        self.api_path = api_path
        self.version = version
        self.request_handler = request_handler
        self.execute_request()
        
    def execute_request(self):
        from oauth.client import OAuthClient
        
        client = OAuthClient('projectplace', self.request_handler)
        
        if self.version != '':
            file_content = client.get_document(self.api_path, version=self.version)
        else:
            file_content = client.get_document(self.api_path)
            
        self.request_handler.response.headers.add_header("Content-Type", client.response_headers['content-type'])
        self.request_handler.response.headers.add_header("Content-Disposition", client.response_headers['content-disposition'])
        
        self.request_handler.response.out.write(file_content)